傳入任意json資料來源進行格式化處理並用Angularjs顯示

來源:互聯網
上載者:User

標籤:

在項目開發中,有些時候我們並不知道傳入的資料來源(只針對於json格式的)的內部結構,本文用最笨的辦法先把資料來源格式化一遍,把所有的key值替換成自訂的值,然後在頁面上用angularjs展示。

html代碼:

<div class="col-xs-12 col-sm-12 col-md-4 col-lg-3 col-hj-div" ng-app=‘hjhhhap‘ ng-controller=‘fgfg_con‘ id="jkjk">
  <table class="table hj-select-pannel-table table-striped table-hover">
    <tbody>
      <tr ng-repeat="hj_test3_data in ghghgh track by $index">
        <td><input type="checkbox"></td>
        <td>{{hj_test3_data.hj_index_1}}</td>
        <td>{{hj_test3_data.hj_index_2}}</td>
      </tr>
    </tbody>
  </table>
</div>

js代碼:

<script>

  

var demoApp = angular.module("hjhhhap",[]);
demoApp.controller("fgfg_con",[‘$scope‘,function($scope){
//console.log(JSON.stringify(ghghgh_result));
var ghghgh_result=hjFormatDataSource(this_company1);
var ghghgh_result1=[{"hj_index_1":"1007","hj_index_2":"邯鄲漢迪","hj_index_3":"111111"},{"hj_index_1":"1008","hj_index_2":"邯鄲樂尚","hj_index_3":"111111"}];

//測試資料來源
$scope.ghghgh=ghghgh_result;
}]);

//如果頁面上已經使用過ng-app了,這裡就得添加這段代碼,啟動該ng-app
angular.element(document).ready(function (){angular.bootstrap(document.getElementById(‘jkjk‘), [‘hjhhhap‘]);});

 

//資料處理階段

function hjFormatDataSource(this_dataSource){
  var hj_data_source=[];

  //遍曆該資料來源
  for(var i=0; i<this_dataSource.length; i++){
    var myjsonStr = "";
    var this_list={};
    var j=1;
    for(var key in this_dataSource[i]){

      //把資料來源的key變成自訂的屬性,方便頁面上顯示
      myjsonStr = setJson(myjsonStr,"hj_index_"+j,this_dataSource[i][key]);
      j++;
    }
    hj_data_source.push(myjsonStr);
  }
  var arr = eval(‘([‘ + hj_data_source + ‘])‘);
  return arr;
}

//添加或者修改json資料
function setJson(jsonStr,name,value)
{
  if(!jsonStr)jsonStr="{}";
  var jsonObj = JSON.parse(jsonStr);
  jsonObj[name] = value;
    return JSON.stringify(jsonObj);
}

function strToJson(str){
  var json = eval(‘(‘ + str + ‘)‘);
  return json;
}

</script>

 

如有不足之處,請多多指教。

 

傳入任意json資料來源進行格式化處理並用Angularjs顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.